
function saveHighScores()
	print("High scores saved!")
end

function printCursor(page)
	local maxOptions = #page.options
	for i=maxOptions, 1, -1 do
		local optionList = page.options[i]
		if (i == page.level) then
			io.write("O  < ")
		else
			io.write("   < ")
		end
		io.write(optionList[page.selections[i]])
		print(" >")
	end
end

function renderPlanetShipyardScreen()
	local shipId = getSelection(page, 3)
	local cost = shipList[shipId].cost
	local tradeInCredit = shipResaleFactor * player.ship.cost
	print("Current ship: " .. player.ship.model)
	print("Current ship trade-in credit: " .. string.format("%d", tradeInCredit))
	print("New ship cost: " .. string.format("%d", cost))
	if (tradeInCredit - cost <= 0) then
		print("Total cost: " .. string.format("%d", cost - tradeInCredit))
	else
		print("Total credit: " .. string.format("%d", tradeInCredit - cost))
	end
	print("Credits: " .. player.credits)
end

function renderPlanetServicesScreen()
	local serviceId = getSelection(page, 2)
	local serviceCost = serviceList[serviceId].cost
	print("Hull Condition: " .. string.format("%d", math.floor(100 * player.ship.hullHealth / player.ship.hullStrength)) .. "%")
	print("Fuel: " .. tostring(player.ship.fuel) .. " / " .. tostring(player.ship.maxFuel))
	print("Service Cost: " .. tostring(serviceCost))
	print("Credits: " .. player.credits)
end

function renderPlanetPlanetsScreen()
	local id = getSelection(page, 3)
	local targetPlanet = planetList[id]
	local distance = calculatePlanetDistance(player.planet, targetPlanet)
	print("Distance: " .. tostring(distance) .. " Miles")
end

function renderPlanetBuyScreen()
	local commodityId = getSelection(page, 3)
	local commodity = commodityList[commodityId]
	print("Stock: " .. player.planet.commodities[commodityId])
	print("Unit Cost: " .. math.floor(player.planet.commodityRates[commodityId] * commodity.baseCost))
	print("In Inventory: " .. player.commodities[commodityId])
	print("Credits: " .. player.credits)
end

function processInput()
	if (not gameLoaded) then
		newGame()
		gameLoaded = true
	end

	if (warp == nil and player ~= nil and player.planet ~= nil) then
		print(player.planet.name)
		print("===")
	end
	if (popUps ~= nil and #popUps > 0) then
		for i, popUp in ipairs(popUps) do
			for j, lineItem in ipairs(popUp) do
				print(lineItem)
			end
		end
		print("Press A to close pop up")
	end
	if (page == nil) then
		return
	elseif (page.id == "Commodities") then
		renderPlanetBuyScreen()
	elseif (page.id == "Planets") then
		renderPlanetPlanetsScreen()
	elseif (page.id == "Services") then
		renderPlanetServicesScreen()
	elseif (page.id == "Shipyard") then
		renderPlanetShipyardScreen()
	end
	printCursor(page)
	io.write("> ")
	local inputCommand = io.read()
	onButtonPress(inputCommand)
end

repeat
	if (warp ~= nil and page == nil) then
		continueWarp(false)
	else
		processInput()
	end
until (exitGame)
